ASP.NET Caching objects

While using the output cache instruction is easy to use and working with it, it only allows you to cache the whole page, or for a complete user control. Although it is okay for some situations, there will be times where an object will be very cautious in caching though the following example may look a bit silly, it shows how to use the cache object to store it for an ArrayList instance with a custom object Can be used to create a new project, or use an existing page, and codebahind page_load method in such a way Let (in this example, you do not need to modify the part markup):


protected void Page_Load(object sender, EventArgs e)
{
    ArrayList datestamps;
    if(Cache["datestamps"] == null)
    {
        datestamps = new ArrayList();
        datestamps.Add(DateTime.Now);
        datestamps.Add(DateTime.Now);
        datestamps.Add(DateTime.Now);

        Cache.Add("datestamps", datestamps, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 60), System.Web.Caching.CacheItemPriority.Default, null);
    }
    else
        datestamps = (ArrayList)Cache["datestamps"];

    foreach(DateTime dt in datestamps)
        Response.Write(dt.ToString() + "<br />");
}

Try running this example and reloading the page. 3 examples of the current date are placed in an ArrayList, and the list is then cached using the cache object. At each page load, the cache is checked to see that our names include our ArrayList using the given name. If it is not, then the list is ready, full and cached. If ArrayList is already in the cache, then we pull it out and put it on the right type (everything is stored as an object type). It does not matter where we get the list from, we repeat through it and output each value.

Most of them should be very straight forward, except that we add the list to the cache. Add method takes a bunch of parameters, and I will try to explain them all.

First of all, the key that we want to store our purpose, in this case we call it "datestamps". The key is used to bring the object back from the cache.

The next parameter is the object that we want to cache, so it is not explained.

Next up is the CacheDependency parameter, it can be used by telling ASP.NET that cached items depend on other items, allowing ASP.NET to cancel the cache, if one of these items changes

The following two parameters show how and when the time expires in the ASP.NET.net cache. The first one specifies a complete ending, which means the item will expire at a certain time of day. This is not usually what you want, so we set it up for the nosolute accession. After this we can specify a sliding end, which means that the item will expire after a certain time, depending on the time when the item is cached. The TimeSpen class is used, and we have specified that it should end after 0 hours, 0 minutes, and 60 seconds. Prioritization of items in the cache can be determined. It is used when ASP.NET is removing items from the cache - items with low priority are first deleted

The last parameter allows us to specify a callback function for the item, which will be asked to you after the item has been removed from the cache, we only specify a zero value if you need it.

Therefore, as you can see, it is really easy to store objects using a cache object. Of course, we do not really earn any performance in this example, but only imagination here is the limit. One of the most common practices is collecting recoverable items from a database in the cache, so save resources on the database machine and allow for fast loading pages.